package main

import (
	"fmt"
	"gitee.com/go-web-2/business/config"
	"gitee.com/go-web-2/business/file_server2"
	"gitee.com/go-web-2/business/file_server2/uploader"
	"gitee.com/go-web-2/business/route"
	"gitee.com/go-web-2/business/user"
	"gitee.com/go-web-2/business/user/login"
	handler2 "gitee.com/go-web-2/web_framework/handler"
	"gitee.com/go-web-2/web_framework/middleware"
	"gitee.com/go-web-2/web_framework/server"
	"net/http"
)

// 登入登出
func registerLoginRoute(s server.Server) {
	// user模块 gin有路由组的概念，可参考（手写web框架未实现
	s.Route(http.MethodPost, route.UserLogin, login.Login)
	s.Route(http.MethodGet, route.UserCurrent, user.GetUserInfo)
	s.Route(http.MethodPost, route.UserLogout, login.OutLogin)
}

// 文件上传下载
func registerFileResourceRoute(s server.Server) {
	directUploader := uploader.NewDirectUploader(config.FileTargetPath)
	s.Route(http.MethodPost, route.FileDirectUpload, directUploader.HandleUpload)

	fragmentUploader := uploader.NewFragmentUploader(config.FileTmpPath, config.FileTargetPath)
	s.Route(http.MethodPost, route.FileFragmentPrepare, fragmentUploader.HandlePrepare)
	s.Route(http.MethodPost, route.FileFragmentUploadSlice, fragmentUploader.HandleUpload)
	s.Route(http.MethodPost, route.FileFragmentFinish, fragmentUploader.HandleFinished)

	downloader := file_server2.NewDownloader(config.FileTargetPath)
	s.Route(http.MethodGet, route.FileDownloadList, downloader.HandleDownloadList)
	s.Route(http.MethodGet, route.FileDownload, downloader.Download)
	// todo: 接入https://www.kkview.cn/zh-cn/index.html
	s.Route(http.MethodGet, route.FilePreview, downloader.Preview)
}

func main() {
	serverConfig := server.HttpServerConfig{
		Name:    "web-server",
		Handler: handler2.NewTree(),
		Middlewares: []middleware.Middleware{
			login.ValidateLogin,
		},
	}

	s := server.NewSdkHttpServer(serverConfig)

	registerLoginRoute(s)
	registerFileResourceRoute(s)

	err := s.Start("127.0.0.1:8080")
	if err != nil {
		fmt.Println(err)
		return
	}
}
